package com.utility.project.controller;

import com.utility.project.common.base.R;
import com.utility.project.dto.CustomeUseAndTime;
import com.utility.project.entity.YsCustomer;
import com.utility.project.param.CustomerParam;
import com.utility.project.param.UsageTimeParam;
import com.utility.project.param.UserAccessParam;
import com.utility.project.service.IYsCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yangzhiguo
 * @date 2024-06-13
 * @description 会员信息接口设计
 */
@Api(tags = "会员信息接口设计")
@RestController
@RequestMapping("/ysCustomer")
public class YsCustomerController {

    @Resource
    private IYsCustomerService ysCustomerService;

    @ApiOperation("通过ID查询会员信息")
    @GetMapping("/queryById")
    public R queryById(@RequestParam("id")Long id){
        return R.ok(ysCustomerService.getById(id));
    }

    @ApiOperation("通过username查询会员信息")
    @GetMapping("/queryByUsername")
    public R queryByUsername(@RequestParam("username")String username){
        YsCustomer ysCustomer = ysCustomerService.queryYsCustomer(username);
        return R.ok(ysCustomer);
    }

    @ApiOperation("新增会员数据")
    @PostMapping("/save")
    public R save(@RequestBody CustomerParam param){
        Boolean res = ysCustomerService.saveCustomer(param);
        if(res) {
            return R.okWithMsg("会员保存成功");
        }else{
            return R.fail("会员保存失败");
        }
    }

    @ApiOperation("领取每日的5次使用次数")
    @PostMapping("/abtainDailyFreeUser")
    public R abtainDailyFreeUser(@RequestBody UsageTimeParam param){
        String username = param.getUsername();
        Boolean res = ysCustomerService.abtainDailyFreeUser(username);
        if(res) {
            return R.okWithMsg("领取成功");
        }else{
            return R.fail("领取失败");
        }
    }

    @ApiOperation("查询用户是否已经领取了每日免费使用次数")
    @GetMapping("/queryDailyFreeUse")
    public R queryDailyFreeUse(@RequestParam("username") @ApiParam("用户名，对应于小程序openId") String username){
        Boolean res = ysCustomerService.queryDailyFreeUse(username);
        if(res) {
            return R.okWithMsg("暂未领取每日免费使用次数");
        }else{
            return R.fail("已经领取每日免费使用次数");
        }
    }

    @ApiOperation("是否有使用次数的权限")
    @PostMapping("/accessToUseOrNo")
    public R accessToUseOrNo(@RequestBody UserAccessParam accessParam){
        Boolean res = ysCustomerService.accessToUseOrNo(accessParam.getUsername(), accessParam.getUseNums());
        if(res) {
            return R.okWithMsg("有使用权限");
        }else{
            return R.fail("没有使用权限");
        }
    }

    @ApiOperation("查询用户的使用次数和过期时间")
    @GetMapping("/queryUseTimeAndValidTime")
    public R queryUseTimeAndValidTime(@RequestParam("username") @ApiParam("用户名，对应于小程序openId") String username){
        CustomeUseAndTime customeUseAndTime = ysCustomerService.queryUseTimeAndValidTime(username);
        return R.ok(customeUseAndTime);
    }
}
